<template>
	<view class="manage-item">
		<view class="tops">
			<view class="top-header">
				<text v-if="item.is_recommend==1">[首页推荐]</text> <text class="name">{{ itemObj.username }}</text>
			</view>
			<uni-icons type="scan" size="20" @click="goCheck"></uni-icons>
		</view>
		<view class="container">
			<view class="row">
				<!-- 状态0=使用中1=已停止2=已关闭3=首页推荐4=置顶 -->
				<text class="label">状态：</text>
				<text class="green">{{status}}</text>
			</view>
			<view class="row">
				<text class="label">排序号：</text><text class=""> <text>{{itemObj.order_num}}</text> <text class="tip0"> (小的先显示)</text> </text>
			</view>
			<view class="row">
				<text class="label">排期：</text><text>{{ useableDay.length }} 天可预约</text>
			</view>
			<view class="row">
				<text class="label">修改：</text><text>{{ itemObj.update_time}}</text>
			</view>
			<view class="btns">
				<view class="btn" @click="goSetting(itemObj)">
					<text class="iconfont icon-shezhi"></text>
					<text>设置</text>
				</view>
				<view class="btn" @click="checkTable(itemObj.id)">
					
					<text>名单与核销</text>
				</view>
				<view class="btn" @click="changeState(itemObj.id)">
					<text class="iconfont icon-zidonghexiao"></text>
					<text>状态</text>
				</view>
				<view class="btn" @click="moreSetting(itemObj.id,ctype)">
					<text>更多</text>
				</view>
			</view>
			<!-- 弹框 -->
			<PopupCom ref="popupcom" @confirm-popup="confirmPopup" @update="update" :state="state">
				<view class="uni-list">
					<view class="uni-list-cell uni-list-cell-pd">
						<view class="uni-list-cell-db">使用中</view>
						<switch :disabled="itemObj.status==0?true:false" :checked="itemObj.status==0?stateT:stateF" @change="myChoose0(0)" />
					</view>
					<view class="uni-list-cell uni-list-cell-pd">
						<view class="uni-list-cell-db">已停止</view>
						<switch :disabled="itemObj.status==1?true:false" :checked="itemObj.status==1?stateT:stateF" @change="myChoose1(1)" />
					</view>
					<view class="uni-list-cell uni-list-cell-pd">
						<view class="uni-list-cell-db">已关闭</view>
						<switch :disabled="itemObj.status==2?true:false" :checked="itemObj.status==2?stateT:stateF" @change="myChoose2(2)" />
					</view>
					
				</view>
			</PopupCom>
		</view>
		<uni-popup ref="share" type="share" safeArea backgroundColor="#fff">
			<uni-popup-share @select="myChoose"></uni-popup-share>
		</uni-popup>
		<!-- <button open-type="share">分享</button> -->
		
	</view>
</template>

<script>
	import * as XLSX from '../../utils/excel.js'
	import { GetLi, DelHelper, UptoTop, ToRecommend } from '../../api/index.js'

	export default {
		name:"ManageItem",
		props:{
			item:{
				type:Object,
				default:()=>{}
			},
			ctype:{
				type:String,
				default:'1'
			}
		},
		data() {
			return {
				id:'',
				show:false,
				type:'center',
				stateF:0,
				stateT:1,
				state:0,
				itemObj:this.item
			};
		},
		computed:{
			useableDay(){
				let arr = this.itemObj.date_time.slice(0,this.itemObj.date_time.length-1).split(',')
				let res = arr.map(item=>{
					return item.split('*')[0]
				})
				// console.log(res,'ffres')
				return res
			},
			status(){
				let is =this.item.status
				// 状态0=使用中1=已停止2=已关闭3=首页推荐4=置顶
				// if(is==0){
				// 	return '使用中'
				// }
				// return this.item.status
				let res
				switch(is){
					case 0:
						res='使用中';
						break;
					case 1:
						res='已停止';
						break;
					case 2:
						res='已关闭';
						break;
				}
				console.log(res,'res')
				return res
			},
			// 修改props: {
				itemProps:{
					get() {
						return this.item
					},
					set(value) {
						this.$emit('update:item',value)
						console.log(value)
					}
				}
			
		},
		watch:{
			'item.status'(value){
				console.log(value,'vvvv')
				this.itemObj.status = value
				this.$emit('updateItem',this.itemObj)
			}
			
		},
		methods:{
			goSetting(item){
				uni.navigateTo({
					url:'/subPackageAdmin/OrderSet/OrderSet?item='+JSON.stringify(item)
				})
			},
			change(e) {
							console.log('当前模式：' + e.type + ',状态：' + e.show);
						},
			// checkTables(){
			// 	const fileName='打卡'
			// 	    const header = ['date', 'defaultType', 'device']
			// 	    const headerName = { date: '相识日期', defaultType: '类型', device: '姓名' }
			// 	    const ExcelData = [{ date: '2022-02-02', defaultType: '御姐', device: '学姐' }, { date: '2022-02-03', defaultType: '萝莉', device: '学妹' }]
			// 	    XLSX.excel_exprot(ExcelData, header, headerName,fileName)
			
			// },
			myChoose0(e){
				console.log(e)
				this.state = e
				if(e){
					this.state=0
				}
			},
			myChoose1(e){
				console.log(e)
				this.state=0
				this.state = e
				
			},
			myChoose2(e){
				console.log(e)
				this.state=0
				this.state = e
				
			},
			confirmPopup(){
				console.log('3333')
			},
			update(e){
				console.log(e,this.state)
				this.item.status =e
			},
			checkTable(id){
				
				uni.showActionSheet({
					itemList:['预约名单','导出名单Excel文件','管理员核销预约码'],
					success: (res) => {
					          console.log('选择了第' + (res.tapIndex + 1) + '个选项');
							  if(res.tapIndex==0){
								  console.log('33')
								  uni.navigateTo({
								  	url:'/subPackageAdmin/OrderTable/OrderTable?id='+ id
								  })
							  }
							  if(res.tapIndex==1){
								  // GetLi().then(res=>{
									 //  console.log(res.data,'ff')
								  // })
								  // uni.$http({
									 //  url:'/api/helper/table',
									 //  method:'GET',
									 //  responseType: 'arraybuffer',
								  // }).then(res=>{
									 //  console.log(res,'g')
								  // })
								 
								  uni.request({
								    url: 'http://192.168.43.120:3002/api/order/list/excel/'+id, // 替换为你的Node.js服务器地址
								    method: 'GET',
									sslVerify:false,
								    responseType: 'arraybuffer', // 设置响应类型为arraybuffer以接收二进制文件
								    success: (res) => {
										let resData = res.data
										console.log(res,'res',resData)
										
								      const fileManagerObj = uni.getFileSystemManager() // 获取全局的文件管理器
								                 console.log(fileManagerObj,'o');
								                 // 文件存储到本地的路径
								                 const filePath = `${wx.env.USER_DATA_PATH}/${new Date().getTime()}.xlsx` 
								                 fileManagerObj.writeFile({
								                     data: resData, // 拿到的arraybuffer数据
								                     filePath: filePath,
								                     encoding: 'binary',
								                     success: (res) => {
								                         console.log(res) // 成功了的话这里会打印 writeFile:ok
								                         this.viewDoc(filePath)
								                     }
								                 })
		
									  
									
								    },
								    fail: (error) => {
								      console.error('导出失败', error);
								    }
								  });
							  }
							  if(res.tapIndex==2){
								  this.goCheck()
								  
							  }
					        },
					        fail: (err) => {
					          console.log('弹窗取消');
					        }
					
				})
				
			},
			// 打开文件
			viewDoc(filePath) {
			    uni.openDocument({
			        // 直接打开
			        filePath: filePath, // 这里填上面写入本地的文件路径
			        fileType: 'xlsx',
			        showMenu: true, // 右上角是否有可以转发分享的功能，配不配随意
			        success: (res) => {
			            console.log('打开文档成功')
			        }
			    })
			},
			// 核销
			goCheck(){
				console.log('res,','ress')
				// uni.scanCode({
				// 	scanType:'qrCode',
				// 	success:function(res){
				// 		console.log(res,'ress')
				// 	}
				// })
				// 允许从相机和相册扫码
				uni.scanCode({
					// scanType:'qrCode',//无反应
					scanType:['qrCode'],
					success: async (res)=> {
						// this.qrcodeText = this.username+' '+this.type+' '+this.date+' '+this.myFormatData.username+' '+this.myFormatData.tel
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						let results = res.result.split('_')
						console.log(results,'results')
						const { data:res1 } = await FindByTelphone(this.item.phone)
						console.log(res1,'res1')
						res1.data.forEach(item=>{
							console.log(item.id,results[3],'ddf')
							if(item.id==results[3]){
								console.log('tttt')
								if(item.status==2){
									return uni.showToast({
										title:'已核销',
										icon:'error'
									})
								}
								if(item.status==3){
									return uni.showToast({
										title:'已过期',
										icon:'error'
									})
								}
								if(item.status==4){
									return uni.showToast({
										title:'系统取消',
										icon:'error'
									})
								}
							// const { data:res } =  CheckoutCode({phone:this.phone,status:2})
							CheckoutCode({phone:this.phone,status:2,id:this.id}).then(res=>{
								console.log(res,'ccc')
								if(res.data.status==0){
									return uni.showToast({
										title:'核销成功',
										icon:'success'
									})
								}
							})
							
						}
						// else{
						// 	console.log('eeee')
						// 	return uni.showToast({
						// 		title:'查询失败未找到',
						// 		icon:'fail'
						// 	})
						// }
						}
						)
						// if(results[0]==res1.data.username){
							
						// 	if(res1.data.status==2){
						// 		return uni.showToast({
						// 			title:'已核销',
						// 			icon:'error'
						// 		})
						// 	}
						// 	if(res1.data.status==3){
						// 		return uni.showToast({
						// 			title:'已过期',
						// 			icon:'error'
						// 		})
						// 	}
						// 	if(res1.data.status==4){
						// 		return uni.showToast({
						// 			title:'系统取消',
						// 			icon:'error'
						// 		})
						// 	}
						// 	const { data:res } = await CheckoutCode({phone:2,status:2})
						// 	console.log(res,'ccc')
						// 	if(res.status==0){
						// 		return uni.showToast({
						// 			title:'核销成功',
						// 			icon:'success'
						// 		})
						// 	}
						// }else{
						// 	return uni.showToast({
						// 		title:'查询失败未找到',
						// 		icon:'fail'
						// 	})
						// }
						}
					
				});
				
				
				// 允许从相机和相册扫码
				// uni.scanCode({
				// 	// scanType:'qrCode',//无反应
				// 	scanType:['qrCode'],
				// 	success: async (res)=> {
				// 		// this.qrcodeText = this.username+' '+this.type+' '+this.date+' '+this.myFormatData.username+' '+this.myFormatData.tel
				// 		console.log('条码类型：' + res.scanType);
				// 		console.log('条码内容：' + res.result);
				// 		let results = res.result.split('_')
				// 		console.log(results,'results')
				// 		const { data:res1 } = await FindByTelphone(2)
				// 		console.log(res1,'res1')
				// 		if(results[0]==res1.data.username){
							
				// 			if(res1.data.type==2){
				// 				return uni.showToast({
				// 					title:'已核销',
				// 					icon:'error'
				// 				})
				// 			}
				// 			if(res1.data.type==3){
				// 				return uni.showToast({
				// 					title:'已过期',
				// 					icon:'error'
				// 				})
				// 			}
				// 			if(res1.data.type==4){
				// 				return uni.showToast({
				// 					title:'系统取消',
				// 					icon:'error'
				// 				})
				// 			}
				// 			const { data:res } = await CheckoutCode({tel:2,type:2})
				// 			console.log(res,'ccc')
				// 			if(res.status==0){
				// 				return uni.showToast({
				// 					title:'核销成功',
				// 					icon:'success'
				// 				})
				// 			}
				// 		}else{
				// 			return uni.showToast({
				// 				title:'查询失败未找到',
				// 				icon:'fail'
				// 			})
				// 		}
				// 	}
				// });
			},
			// 修改状态
			changeState(id){
				this.$refs.popupcom.open()
				this.id = id
				console.log(id,'id',this.id)
					// this.show = true
					// console.log(this.$refs.popupcom.open())
				
			},
			// 更多
			moreSetting(id,ctype){
				console.log(id,ctype,'ctype')
				this.id = id
				let that = this
				// 动态
				let items = ['首页推荐','置顶','删除','分享']
				if(ctype=='1'){
					items = items
				}else
				if(ctype=='2'){
					items[0]='取消推荐'
				}else
				if(ctype=='3'){
					items[1]='取消置顶'
				}
				uni.showActionSheet({
					itemList:items,
					success(e) {
						console.log(e,'e',items)
						if(e.tapIndex==0&&items[e.tapIndex]=='取消推荐'){
							console.log('取消推荐')
							return that.toRecommend(that.id,0)
						}
						if(e.tapIndex==0&&items[e.tapIndex]=='首页推荐'){
							console.log('推荐')
							return that.toRecommend(that.id,1)
						}
						if(e.tapIndex==1&&items[e.tapIndex]=='取消置顶'){
							console.log('取消置顶')
							return that.upToTop(that.id,0)
						}
						if(e.tapIndex==1&&items[e.tapIndex]=='置顶'){
							console.log('置顶')
							return that.upToTop(that.id,1)
						}
						if(e.tapIndex==2){
							return that.delHelper(that.id)
						}
						if(e.tapIndex==3){
							return that.shareToggle()
						}
					}
					
				})
			},
			// 删除
			async delHelper(id){
				const { data:res } = await DelHelper(id)
				console.log(res,'del')
				let that = this
				if(res.status==0){
					this.$emit('updateHelper',{id:that.id,type:'del'})
					uni.showToast({
						title:res.message,
						icon:'success',
						success() {
							console.log('SSS',that)
							
						}
					})
				}
			},
			// 分享
			shareToggle() {
				this.$refs.share.open()
			},
			// 置顶
			async upToTop(id,tp){
				let paramsData = {
					is_up:tp
				}
				const { data:res } = await UptoTop(id,paramsData)
				console.log(res,'置顶')
				// this.$emit('updateHelper',{id:this.id,type:'top'})
				if(res.status==0&&tp==1){
					this.$emit('updateHelper',{id:this.id,type:'top'})
					uni.showToast({
						title:res.message,
						icon:'success'
					})
				}else{
					this.$emit('updateHelper',{id:this.id,type:'top'})
					uni.showToast({
						title:res.message,
						icon:'success'
					})
				}
			},
			// 首页推荐
			async toRecommend(id,tp){
				let paramsData = {
					is_recommend:tp
				}
				const { data:res } = await ToRecommend(id,paramsData)
				console.log(res,'首页推荐')
				// this.$emit('updateHelper',{id:this.id,type:'rec'})
				if(res.status==0&&tp==1){
					this.$emit('updateHelper',{id:this.id,type:'rec'})
					uni.showToast({
						title:res.message,
						icon:'success'
					})
				}else{
					this.$emit('updateHelper',{id:this.id,type:'rec'})
					uni.showToast({
						title:res.message,
						icon:'success'
					})
				}
			},
			// 选择
			myChoose(e){
				console.log(e,'dd')
				if(e.index==0){
					// uni.share({
					//     provider: "weixin",
					//     scene: "WXSceneSession", // 或 "WXSenceTimeline"
					//     type: 5,
					//     title: "分享的标题",
					//     summary: "分享的摘要",
					//     miniProgram: {
					//         id: "小程序原始id",
					//         path: "/path/to/page",
					//         webUrl: "兼容低版本的网页链接",
					//         type: 0
					//     },
					//     success: function (res) {
					//         console.log("分享成功");
					//     },
					//     fail: function (err) {
					//         console.log("分享失败：" + JSON.stringify(err));
					//     }
					// });
					
					// uni.share({
					// 	provider: "weixin",
					// 	scene: "WXSceneSession",
					// 	type: 1,
					// 	summary: "我正在使用HBuilderX开发uni-app，赶紧跟我一起来体验！",
					// 	success: function (res) {
					// 		console.log("success:" + JSON.stringify(res));
					// 	},
					// 	fail: function (err) {
					// 		console.log("fail:" + JSON.stringify(err));
					// 	}
					// });
					
					uni.share({
										provider: "weixin",
										title:"uniapp",
										scene: "WXSceneSession",
										type: 1,
										summary: "我正在使用HBuilderX开发uni-app，赶紧跟我一起来体验！",
										success: function (res) {
											console.log("success:" + JSON.stringify(res));
										},
										fail: function (err) {
											console.log("fail:" + JSON.stringify(err));
										}
									})
					
				}
				if(e.index==1){
					
				}
				if(e.index==2){
					
				}
			}
		
		
		}
	}
</script>

<style lang="scss" scoped>
.uni-list{
	display: flex;
	justify-content: space-evenly;
	padding-top: 100rpx;
}
.popup-content{
	width: 600rpx;
	height: 400rpx;
	border-radius: 15rpx;
	margin: auto auto;
	padding: 30rpx;
	.brns-row{
		display: flex;
		.close{
			flex: 1;
		}
		.confirm{
			flex: 1;
		}
	}
}
.manage-item{
	width: 100%;
	margin-bottom: 30rpx;
	border: 1px solid #eee;
	border-radius: 10rpx;
	// border: 1px solid #000;
	
	.tops{
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		background: #eee;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 10rpx;
		box-sizing: border-box;
		// border: 1px solid #000;
		.top-header{
			// height: 80rpx;
			
			.name{
				font-weight: bold;
				margin-left: 10rpx;
			}
		}
	}
	.container{
		background: #fff;
		padding-bottom: 30rpx;
		.row{
			margin-left: 50rpx;
			line-height: 60rpx;
			height: 60rpx;
			.label{
				font-weight: bold;
			}
		}
		.btns{
			display: flex;
			justify-content: space-around;
			margin-top: 20rpx;
			.btn{
				height: 70rpx;
				line-height: 70rpx;
				background: #e1e1e1;
				padding: 0rpx 20rpx;
				border-radius: 10rpx;
				.iconfont{
					margin-right: 6rpx;
				}
				.icon-zidonghexiao{
					color: orange!important;
				}
			}
		}
	}
}
</style>